<template>
    <div>
        <h3>修改会员</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">会员编号</td>
                    <td>
                        <input type="text" v-model="queryInfo.MemberCode">
                    </td>
                </tr>

                <tr>
                    <td align="right">手机号</td>
                    <td>
                        <input type="text" v-model="queryInfo.Phone" maxlength="11">
                    </td>
                </tr>

                <tr>
                    <td align="right">姓名</td>
                    <td>
                        <input type="text" v-model="queryInfo.UesrName">
                    </td>
                </tr>

                <tr>
                    <td align="right">身份证号</td>
                    <td>
                        <input type="text" v-model="queryInfo.Card" maxlength="18">
                    </td>
                </tr>

                <tr>
                    <td align="right">会员等级</td>
                    <td>
                        <select v-model="queryInfo.LevelId">
                            <option value="0">请选择</option>
                            <option :value="item.Id" v-for="item in data.list">{{ item.LevelName }}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right">会员生日</td>
                    <td>
                        <input type="date" v-model="queryInfo.Birthday" />
                    </td>
                </tr>

                <tr>
                    <td align="right">性别</td>
                    <td>
                        <input type="radio" name="Sex" :value="1" v-model="queryInfo.Sex">男
                        <input type="radio" name="Sex" :value="0" v-model="queryInfo.Sex">女
                    </td>
                </tr>


                <tr>
                    <td align="right">积分</td>
                    <td>
                        <input type="text" v-model="queryInfo.JiFen">
                    </td>
                </tr>


                <tr>
                    <td align="right">余额</td>
                    <td>
                        <input type="text" v-model="queryInfo.Money">
                    </td>
                </tr>

                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="修改" @click="ok" class="btn btn-primary">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">

import axios from 'axios';
import { ref, reactive, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import moment from 'moment';

onMounted(() => {
    getDate();
    getList();
})

const route = useRoute();

//根据传递过来的id实现反填
const getDate = () => {

    axios({
        method: 'get',
        url: '/api/Member/GetMemberModelItem',
        params: {
            id: route.query.id,
        },
    })
        .then(res => {
            console.log(res);
            queryInfo.MemberCode = res.data.MemberCode;
            queryInfo.Phone = res.data.Phone;
            queryInfo.UesrName = res.data.UesrName;
            queryInfo.Card = res.data.Card;
            queryInfo.LevelId = res.data.LevelId;
            queryInfo.Birthday = res.data.Birthday.substring(0, 10);
            queryInfo.Sex = res.data.Sex;
            queryInfo.JiFen = res.data.JiFen;
            queryInfo.Money = res.data.Money;
        })
        .catch(err => {
            console.log(err);
        })
}


//存储等级列表
let data: any = reactive({
    list: []
})

//绑定下拉框
const getList = () => {
    axios({
        method: 'get',
        url: '/api/Member/GetMemberLevelList',
    })
        .then(res => {
            data.list = res.data;
        })
        .catch(err => {
            console.log(err);
        })
}


// var num=parseInt(Math.random()*1000+100);
var num = Math.random().toString().substring(2, 5);
console.log(num)
const router = useRouter();

let queryInfo: any = reactive({
    "Id": route.query.id,
    "MemberCode": moment(new Date()).format("YYYYMMDD") + num,
    "Phone": "",
    "UesrName": "",
    "Card": "",
    "LevelId": 0,
    "Birthday": "",
    "Sex": 1,
    "JiFen": '',
    "Money": ''
})

const ok = () => {
    //1.有效验证
    if (queryInfo.Phone == "") {
        alert('手机号不能为空');
        return;
    }
    let reg = /^1[3-9]\d{9}$/;
    if (reg.test(queryInfo.Phone) == false) {
        alert('手机号不合法');
        return;
    }
    if (queryInfo.useName == "") {
        alert('姓名不能为空');
        return;
    }

    if (queryInfo.Card == "") {
        alert('身份证号不能为空');
        return;
    }
    let reg1 = /^\d{17}[0-9xX]$/;
    if (reg1.test(queryInfo.Card) == false) {
        alert('身份证号格式不正确')
        return;
    }

    if (queryInfo.LevelId == "") {
        alert('等级必选');
        return;
    }
    if (queryInfo.Birthday == "") {
        alert('生日不能为空');
        return;
    }
    if (queryInfo.JiFen == "") {
        alert('积分不能为空');
        return;
    }
    if (isNaN(queryInfo.JiFen)) {
        alert('积分只能为数值');
        return;
    }
    if (queryInfo.Money == "") {
        alert('余额不能为空');
        return;
    }
    if (isNaN(queryInfo.Money)) {
        alert('余额必须是数值');
        return;
    }


    axios({
        method: "post",
        url: '/api/Member/UpdateMemberItem',
        data: queryInfo
    })
        .then(res => {
            console.log(res);
            if (res.data == -1) {
                alert('身份证号重复')
            }
            else if (res.data > 0) {
                alert('修改成功');
                router.push('/memberlist')
            }
            else {
                alert('修改失败');
            }

        })
        .catch(err => {
            console.log(err);
        })
}


















</script>

















<style scoped></style>